package com.zenithmind.article.pojo.domain;

import com.zenithmind.common.base.BaseEntity;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

import jakarta.validation.constraints.NotBlank;
import java.time.LocalDateTime;

/**
 * 文章点赞实体
 * 遵循单一职责原则：专注于点赞关系管理
 * 遵循封装原则：通过方法控制点赞状态
 * 遵循值对象模式：表示用户对文章的点赞关系
 */
@EqualsAndHashCode(callSuper = true)
@Getter
@NoArgsConstructor
@Schema(name = "文章点赞实体", description = "文章点赞关系实体")
public class ArticleLike extends BaseEntity {

    @NotBlank(message = "文章ID不能为空")
    @Schema(description = "文章ID")
    private String articleId;

    @NotBlank(message = "用户ID不能为空")
    @Schema(description = "用户ID")
    private String userId;

    @Schema(description = "点赞状态")
    private LikeStatus status = LikeStatus.LIKED;

    @Schema(description = "点赞时间")
    private LocalDateTime likeTime;

    /**
     * 点赞状态枚举
     */
    public enum LikeStatus {
        LIKED(1, "已点赞"),
        CANCELLED(0, "已取消");

        private final Integer code;
        private final String description;

        LikeStatus(Integer code, String description) {
            this.code = code;
            this.description = description;
        }

        public Integer getCode() {
            return code;
        }

        public String getDescription() {
            return description;
        }

        public static LikeStatus fromCode(Integer code) {
            if (code == null) return LIKED;
            for (LikeStatus status : values()) {
                if (status.code.equals(code)) {
                    return status;
                }
            }
            return LIKED;
        }
    }

    // === 构造方法 ===

    /**
     * 创建点赞记录
     */
    public ArticleLike(String articleId, String userId) {
        this.articleId = articleId;
        this.userId = userId;
        this.status = LikeStatus.LIKED;
        this.likeTime = LocalDateTime.now();
    }

    // === 业务方法 ===

    /**
     * 点赞
     */
    public void like() {
        this.status = LikeStatus.LIKED;
        this.likeTime = LocalDateTime.now();
    }

    /**
     * 取消点赞
     */
    public void cancel() {
        this.status = LikeStatus.CANCELLED;
    }

    /**
     * 切换点赞状态
     */
    public void toggle() {
        if (this.status == LikeStatus.LIKED) {
            cancel();
        } else {
            like();
        }
    }

    /**
     * 检查是否已点赞
     */
    public boolean isLiked() {
        return this.status == LikeStatus.LIKED;
    }

    /**
     * 检查是否已取消
     */
    public boolean isCancelled() {
        return this.status == LikeStatus.CANCELLED;
    }

    /**
     * 获取状态码（用于数据库存储）
     */
    public Integer getStatusCode() {
        return this.status.getCode();
    }

    /**
     * 设置状态（用于数据库读取）
     */
    public void setStatusCode(Integer statusCode) {
        this.status = LikeStatus.fromCode(statusCode);
    }

    /**
     * 创建业务标识
     * 用于缓存键和唯一性约束
     */
    public String getBusinessKey() {
        return this.articleId + ":" + this.userId;
    }

    /**
     * 设置文章ID和用户ID（用于创建点赞记录）
     */
    public void setArticleAndUser(String articleId, String userId) {
        this.articleId = articleId;
        this.userId = userId;
    }
}
